var util = require('../utils/util');
var daoUtil = require('../utils/daoUtil');
var commentClass = require('../class/comment');

module.exports = {

    create: function (params, cb) {
        var validate = daoUtil.validate(['context', 'createUid','name','type'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var comment = new commentClass();
            params.enable = 1;
            comment.init(params);
            models.comment.create(comment.getParams(), function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    data.createAt = util.dateFormat(data.createAt);
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    update: function (params,cb) {
        var validate = daoUtil.validate(['rowId','context'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        var updateItems = daoUtil.getParams(['context'],params).has;
        daoUtil.proxy(function(models){
            models.comment.update({rowId:params.rowId},updateItems).exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    del: function (params,cb) {
        var validate = daoUtil.validate(['rowId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.comment.update({rowId:params.rowId},{enable:0}).exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    getListByMconference: function (params, cb) {
        var that = this;
        var validate = daoUtil.validate(['rowId','type'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.comment.find({where:{mconference:params.rowId,type:params.type,enable:1},sort:'createdAt desc'}).populate('createUid').exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    }

};